package com.kukudexin;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

/**
 * 模拟处理跟业务系统对接
 */
public class ActivitiBusinessDemo {

    /**
     * 添加业务key 到Activiti的表
     */
    @Test
    public void addBusinessKey(){
        ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml", "processEngineConfiguration").buildProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance instance
                = runtimeService.startProcessInstanceByKey("myEvection", "1001");
        System.out.println("businessKey ==" + instance.getBusinessKey());
    }
    /**
     * 全部流程实例挂起和激活
     */
    @Test
    public void suspendAllProcessInstance() {
        ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml", "processEngineConfiguration").buildProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("myEvection").singleResult();
        boolean suspended = processDefinition.isSuspended();

        String id = processDefinition.getId();
//        6、如果是挂起状态，改为激活状态
        if(suspended){
            //            如果是挂起，可以执行激活的操作,参数1：流程定义id 参数2：是否激活，参数3：激活时间
            repositoryService.activateProcessDefinitionById(id,true,null);
            System.out.println("流程定义id:"+id+"，已激活");
        }else {
            repositoryService.suspendProcessDefinitionById(id,true,null);
            System.out.println("流程定义id:"+id+"，已挂起");

        }
    }

    /**
     * 挂起激活单个流程  注意如果流程实例已经被挂起了就不能执行当前的流程实例
     */
    @Test
    public void suspendSingleProcessInstance(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId("20001").singleResult();
        boolean suspended = instance.isSuspended();
//        5、获取流程实例id
        String instanceId = instance.getId();
//        6、判断是否已经暂停，如果已经暂停，就执行激活操作
        if(suspended){
//            如果已经暂停，就执行激活
            runtimeService.activateProcessInstanceById(instanceId);
            System.out.println("流程实例id:"+instanceId+"已经激活");
        }else {
//        7、如果是激活状态，就执行暂停操作
            runtimeService.suspendProcessInstanceById(instanceId);
            System.out.println("流程实例id:"+instanceId+"已经暂停");
        }
    }

    @Test
    public void compleTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        2、获取TaskService
        TaskService taskService = processEngine.getTaskService();
//        3、使用taskservice获取任务,参数 流程实例的id，负责人
        Task task = taskService.createTaskQuery()
                .processInstanceId("20001")
                .taskAssignee("jack")
                .singleResult();
        System.out.println("流程实例id=="+task.getProcessInstanceId());
        System.out.println("流程任务id=="+task.getId());
        System.out.println("负责人=="+task.getAssignee());
        System.out.println("任务名称=="+task.getName());
//        4、根据任务的id完成任务
        taskService.complete(task.getId());
    }
}
